package com.oa.tools.beans;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.access.intercept.FilterSecurityInterceptor;

import com.oa.tools.utils.SecurityUtil;

@Configuration
public class BrowerSecurityConfig extends WebSecurityConfigurerAdapter {

	@Autowired
	MyUserDetailsService userDetailsService;
	
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin()                    //  定义当需要用户登录时候，转到的登录页面。
                .and()
                .authorizeRequests()        // 定义哪些URL需要被保护、哪些不需要被保护
                .antMatchers("/config","/config/update","/flow/**",
                		"/fee/wyc/export/**",
                		"/fee/wyc/audit/**",
                		"/fee/wyc/publish/export/**",
                		"/fee/wyc/jiayou/**",
                		"/fee/wyc/xiche/**",
                		"/fee/export")
                //.anyRequest()             // 任何请求,登录后可以访问
                .authenticated();
        
        http.csrf().disable();
    }
    
    

	@Override
	public void configure(WebSecurity web) throws Exception {
		web.ignoring().antMatchers("/lbpm/**","/org/**");
	}



	@Override
	protected void configure(AuthenticationManagerBuilder auth) throws Exception {
		auth.userDetailsService(userDetailsService).passwordEncoder(new PasswordEncoder(){

	        @Override
	        public String encode(CharSequence rawPassword) {
	            return SecurityUtil.MD5(rawPassword.toString());
	        }
	
	        @Override
	        public boolean matches(CharSequence rawPassword, String encodedPassword) {
	            return encodedPassword.equals(SecurityUtil.MD5(rawPassword.toString()));
	        }}
		); 
		
	}
    
    
}
